'use strict';
const gulp = require("gulp");
const minify = require('gulp-minify');
const ts = require('gulp-typescript');
const tsProject = ts.createProject('tsconfig.json');
const inject = require('gulp-inject-string');

const clean = require('gulp-clean');
const uglify = require('gulp-uglify')
const concat = require('gulp-concat');

const browserify = require('browserify')
const tsify = require('tsify')

const buffer = require('vinyl-buffer')
const source = require('vinyl-source-stream')
// const deleteLines = require('gulp-delete-lines')
const deleteLines = require('./gulpDeleteLines')
const typeGlobal = require('./gulpTypeGlobal')



gulp.task('clean', function () {
    return gulp
        .src('dist', {
            read: false,
            allowEmpty: true
        })
        .pipe(clean('dist'));
});

gulp.task('tsc', () => {
    return gulp
        .src(['./src/cc/**/*.ts'])
        .pipe(ts({
            declaration: true,
            lib: ['ES2019', 'dom'],
        }))
        .pipe(deleteLines({
            filters: [/import(.+?);/g]
        }))
        .pipe(typeGlobal.replace())
        .pipe(deleteLines({
            filters: [/constructor[^\)]*\)/]
            // filters: [/constructor\(([\s\S]+?)\);/]
        }))
        .pipe(deleteLines({
            filters: [/private(.+?)/]
        }))
        .pipe(gulp.dest('dist/types'))
})

gulp.task('tsc-extends', () => {
    return gulp
        .src(['dist/index.d.ts'])
        .pipe(typeGlobal.replaceName())
        .pipe(gulp.dest('dist'))
})

gulp.task('tsc-concat', () => {
    return gulp
        .src(['dist/types/**/*.d.ts'])
        .pipe(concat('index.d.ts'))
        .pipe(gulp.dest('dist'))
})
gulp.task('tsc-concat-types', () => {
    return gulp
        .src(['dist/index.d.ts', 'src/**/*.d.ts'])
        .pipe(concat('index.d.ts'))
        .pipe(gulp.dest('dist'))
})

gulp.task('clean-js', function () {
    return gulp
        .src('dist/types/*.js', {
            read: false
        })
        .pipe(clean('*.js'));
});

gulp.task('clean-js-tools', function () {
    return gulp
        .src('dist/tools-javascript@1.0.11.js', {
            read: false
        })
        .pipe(clean('*.js'));
});

gulp.task('clean-types', function () {
    return gulp
        .src('dist/types', {
            read: false,
            allowEmpty: true
        })
        .pipe(clean('dist/types'));
});

gulp.task('buildJs', () => {
    return tsProject.src()
        .pipe(tsProject())
        .pipe(inject.prepend(`let map = {};
        let ar = [];
        const define = function (name, names, cb) {
            let exports = {}
            let cbs = names.map(name => map[name])
            cbs[1] = exports
            cb(...cbs)
            map[name] = exports
        };\n`))
        .pipe(inject.replace('var', 'let'))
        .pipe(deleteLines({
            filters: [/Object.defineProperty\(exports,/]
        }))
        .pipe(deleteLines({
            filters: [/use strict/]
        }))
        .pipe(minify({
            ext: {
                min: ".min.js"
            }
        }))
        .pipe(gulp.dest('./dist'));
})

gulp.task('build', gulp.series(
    gulp.parallel('clean'),
    gulp.parallel('tsc'),
    gulp.parallel('tsc-concat'),
    gulp.parallel('clean-types'),
    gulp.parallel('buildJs'),
    gulp.parallel('tsc-extends'),
    gulp.parallel('clean-js-tools'),
    gulp.parallel('tsc-concat-types'),
))